//
//  settigLayer.m
//  zhuan
//
//  Created by zhao ys on 6/11/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "settingLayer.h"
#import "HelloWorldLayer.h"
#import "GameLogic.h"
#import "SoundManager.h"

@implementation settingLayer
+(CCScene *) scene {
	// 'scene' is an autorelease object.
	CCScene *scene = [CCScene node];
	
	// 'layer' is an autorelease object.
	settingLayer *layer = [settingLayer node];
	
	// add layer as a child to scene
	[scene addChild: layer];
	
	// return the scene
	return scene;
}

// on "init" you need to initialize your instance

#define MENU_COLOR ccc3(67, 99, 0)
#define LABEL_COLOR ccc3(64, 0, 0)

-(CCMenuItemToggle *) CreateToggleMenuItem:(NSString*)title  target:(id)target selector:(SEL)selector {	
	[CCMenuItemFont setFontName:@"Marker Felt"];
	[CCMenuItemFont setFontSize:48];

	CCMenuItemFont * menuitem1 = [CCMenuItemFont itemFromString:[NSString stringWithFormat:@"%@:   on", title]];
	CCMenuItemFont * menuitem2 = [CCMenuItemFont itemFromString:[NSString stringWithFormat:@"%@:  off", title]];
	menuitem1.color = LABEL_COLOR;
	menuitem2.color = LABEL_COLOR;
	
	CCMenuItemToggle * menuitem = [CCMenuItemToggle itemWithTarget:target selector:selector items:menuitem1, menuitem2, nil];
	return menuitem;
}

-(CCMenuItem *) menuWithPng:(NSString*)png target:(id)target selector:(SEL)selector {
	CCSprite * button_normal = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:png]];
	CCSprite * button_select = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:png]];
	CCMenuItem * button = [CCMenuItemSprite itemFromNormalSprite:button_normal selectedSprite:button_select target:target selector:selector];
	return button;
}

-(id) init
{
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super" return value
	if( (self=[super init])) {
		// ask director the the window size
		[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"dkz.plist"];

		
		CGSize size = [[CCDirector sharedDirector] winSize];
		NSString *path = [CCFileUtils fullPathFromRelativePath:@"game_config.plist"];
		NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
		
		//load back ground
		CCSpriteFrame * bgframe = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"dkz_startmenu_bg.png"];
		CCSprite * bg = [CCSprite spriteWithSpriteFrame:bgframe];
		bg.position = ccp(size.width / 2, size.height / 2);
		[self addChild:bg z:-1];
		
		
		//label
		CCSprite * label = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"dkz_option_001.png"]];
		label.position = ccp(size.width / 2, 678);
		[self addChild:label];
		
		//trinket
		CCSprite * trinket = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"dkz_trinkets_002.png"]];
		trinket.position = ccp(850, 500);
		[self addChild:trinket];
		
		
		//menu
		CCSprite * menu_bg = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"dkz_jiesuan_bg.png"]];
		menu_bg.position = ccp(size.width / 2, size.height / 2 - 30);
		[self addChild:menu_bg];
		
		//menu
		sound_on = [[dict objectForKey:@"sound"] boolValue];	
		music_on = [[dict objectForKey:@"music"] boolValue];
		game_center_on = [[dict objectForKey:@"game center"] boolValue];;
		CGSize mbSize = menu_bg.contentSize;
		
		CCMenuItemToggle * menu_sound = [self CreateToggleMenuItem:@"sound" target:self selector:@selector(OnMenuSound:)];
		if (!sound_on) {menu_sound.selectedIndex = 1;}
		menu_sound.position = ccp(mbSize.width / 2, mbSize.height / 2 + 40);

		CCMenuItemToggle * menu_music = [self CreateToggleMenuItem:@"music" target:self selector:@selector(OnMenuMusic:)];
		if (!music_on) {menu_music.selectedIndex = 1;}
		menu_music.position = ccp(mbSize.width / 2, mbSize.height / 2 - 20);

		CCMenuItemToggle * menu_gamecenter = [self CreateToggleMenuItem:@"game center" target:self selector:@selector(OnMenuGameCenter:)];
		if (!game_center_on) {menu_gamecenter.selectedIndex = 1;}
		menu_gamecenter.position = ccp(mbSize.width / 2, mbSize.height / 2 - 80);
			
		CCMenuItemToggle * menu_autoplay = [self CreateToggleMenuItem:@"auto play" target:self selector:@selector(OnMenuAutoPlay:)];
		if (!auto_play_on) {menu_autoplay.selectedIndex = 1;}
		menu_autoplay.position = ccp(mbSize.width / 2, mbSize.height / 2 - 140);
			
		CCMenu * menu = [CCMenu menuWithItems:menu_sound, menu_music, menu_gamecenter, menu_autoplay, nil];
		menu.position = ccp(0, 0);
		//[menu alignItemsVerticallyWithPadding:30];
		[menu_bg addChild:menu];
		
		//review
		menu_review_ = [self menuWithPng:@"dkz_review.png" target:self selector:@selector(OnMenuReview:)];
		
		//suport
		menu_support_ = [self menuWithPng:@"dkz_support.png" target:self selector:@selector(OnMenuSupport:)];

		CCMenu * menu_x = [CCMenu menuWithItems:menu_review_, menu_support_, nil];
		menu_x.position = ccp(0,0);
		[self addChild:menu_x];
		
		{
			CCSprite * sprite_back = [CCSprite spriteWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"dkz_back.png"]];
			CCMenuItem * button_back = [CCMenuItemSprite itemFromNormalSprite:sprite_back selectedSprite:nil target:self selector:@selector(OnMenuBack:)];
			button_back.position = ccp(size.width - 30, size.height - 30);
			CCMenu * menu_back = [CCMenu menuWithItems:button_back, nil];
			menu_back.position = ccp(0,0);
			[self addChild:menu_back];
		}
	}
	return self;
}

-(void) onEnter {
	NSLog(@"settingLayer OnEnter");
	[super onEnter];
	CGSize size = [[CCDirector sharedDirector] winSize];

	float duration = 0.5;

	CGSize rSize = menu_review_.contentSize;
	menu_review_.position = ccp(-rSize.width / 2,500);
	menu_review_.rotation = -90;
	//[menu_review_ runAction:[CCRotateTo actionWithDuration:duration angle:-30]];
	//[menu_review_ runAction:[CCEaseIn actionWithAction:[CCMoveTo actionWithDuration:duration position:ccp(300, 500)] rate:2]];
	
	[menu_review_ runAction:[CCSequence actions:[CCRotateTo actionWithDuration:0.2 angle:-90],
							  [CCRotateTo actionWithDuration:duration angle:-30], nil]];
	[menu_review_ runAction:[
							  CCSequence actions:[CCMoveTo actionWithDuration:0.2 position:ccp(-rSize.width / 2, 500)],
							  [CCEaseIn actionWithAction:[CCMoveTo actionWithDuration:duration position:ccp(300,500)] rate:3],
							  nil]
	 ];
	
	
	CGSize sSize = menu_support_.contentSize;
	menu_support_.position = ccp(size.width + sSize.width / 2, 200);
	menu_support_.rotation = 90;
	[menu_support_ runAction:[CCSequence actions:[CCRotateTo actionWithDuration:duration angle:90],
							  [CCRotateTo actionWithDuration:duration angle:20], nil]];
	[menu_support_ runAction:[
							  CCSequence actions:[CCMoveTo actionWithDuration:duration position:ccp(size.width + sSize.width / 2, 200)],
								[CCEaseIn actionWithAction:[CCMoveTo actionWithDuration:duration position:ccp(800,200)] rate:3],
								nil]
	                         ];
}

-(void) OnMenuSound:(id)sender {
	if (sound_on) [[SoundManager sharedManager] play:@"option_change" loops:1];
	sound_on = !sound_on;
	CCMenuItemToggle * menu = (CCMenuItemToggle*)sender;
	if (!sound_on) {
		menu.selectedIndex = 1;
	} else {
		menu.selectedIndex = 0;
	}
	
	NSString *path = [CCFileUtils fullPathFromRelativePath:@"game_config.plist"];
	NSMutableDictionary * dict =[[NSMutableDictionary alloc] initWithContentsOfFile:path];
	[dict setValue:[NSNumber  numberWithBool:sound_on] forKey:@"sound"];
	[dict writeToFile:path atomically:YES];
	[dict release];
}

-(void) OnMenuMusic:(id)sender {
	if (sound_on) [[SoundManager sharedManager] play:@"option_change" loops:1];

	music_on = !music_on;
	CCMenuItemToggle * menu = (CCMenuItemToggle*)sender;
	if (!music_on) {
		menu.selectedIndex = 1;
		[[SoundManager sharedManager] stop:@"background"];
		[[SoundManager sharedManager] stop:@"background_menu"];

	} else {
		menu.selectedIndex = 0;
		[[SoundManager sharedManager] play:@"background_menu" loops:-1];
	}

	NSString *path = [CCFileUtils fullPathFromRelativePath:@"game_config.plist"];
	NSMutableDictionary * dict =[[NSMutableDictionary alloc] initWithContentsOfFile:path];
	[dict setValue:[NSNumber  numberWithBool:music_on] forKey:@"music"];
	[dict writeToFile:path atomically:YES];
	[dict release];
}

-(void) OnMenuGameCenter:(id)sender {
	if (sound_on) [[SoundManager sharedManager] play:@"option_change" loops:1];

	game_center_on = !game_center_on;
	CCMenuItemToggle * menu = (CCMenuItemToggle*)sender;
	if (!game_center_on) {
		menu.selectedIndex = 1;
	} else {
		menu.selectedIndex = 0;
	}
	
	NSString *path = [CCFileUtils fullPathFromRelativePath:@"game_config.plist"];
	NSMutableDictionary * dict =[[NSMutableDictionary alloc] initWithContentsOfFile:path];
	[dict setValue:[NSNumber  numberWithBool:game_center_on] forKey:@"game center"];
	[dict writeToFile:path atomically:YES];
	[dict release];
}

-(void) OnMenuAutoPlay:(id)sender {
	if (sound_on) [[SoundManager sharedManager] play:@"option_change" loops:1];
	
	auto_play_on = !auto_play_on;
	CCMenuItemToggle * menu = (CCMenuItemToggle*)sender;
	if (!auto_play_on) {
		menu.selectedIndex = 1;
	} else {
		menu.selectedIndex = 0;
	}
}


-(void)OnMenuBack:(id)sender
{  
	//CCTransitionFade
	if (sound_on) [[SoundManager sharedManager] play:@"enter" loops:1];


	CCLayerColor *l = [CCLayerColor layerWithColor:ccc4(0,0,0, 0)];
	//[inScene_ setVisible: NO];
	
	[self addChild:l z:2];

	CCActionInterval *a = [CCSequence actions:
						   [CCFadeIn actionWithDuration:0.5 / 2],
						   [CCCallFunc actionWithTarget:self selector:@selector(popCurScene)],
//						   [CCFadeOut actionWithDuration:duration_/2],
//						   [CCCallFunc actionWithTarget:self selector:@selector(finish)],
						   nil ];
	[l runAction: a];
}

-(void)OnMenuSupport:(id)sender {
	if (sound_on) [[SoundManager sharedManager] play:@"enter" loops:1];
	NSString *str = @"mailto://legend_studio@foxmail.com";  
	[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}

-(void)OnMenuReview:(id)sender {
	if (sound_on) [[SoundManager sharedManager] play:@"enter" loops:1];
	//评分
	NSString *str = @"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=443651716";  
	[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}

-(void)popCurScene {
    [[CCDirector sharedDirector] popScene];
}

@end
